package com.wntms.wy.dao;

import com.wntms.wy.pojo.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SalesmanDao {
  /*
1运输
2中转
3到货
4.司机填完回执

5(已发送短信给收货客户)提货
6(客户验收成功)已提
7验收失败，填写了差错信息
*/
    @Select("select count(*) from salesman_accept where fid=#{fid}")
    int findSACount(String fid);
    //增加验收单前先根据fid判断有没这个货运单
    @Select("select count(*) from freight_contract where fid=#{fid}")
    int freightContractCountFindByFid(String fid);
    //根据fid去查cid
    @Select("select cid from carriage_contract where fid=#{fid}")
    String fidFindCid(String fid);
    //业务员增加验收记录 需要填货运单编号、是否验收成功、货物送达实际时间和是否需要中转，运输合同号通过fid去查得到的
    @Insert("insert into salesman_accept (fid,cid,ispass,arrivetime,istransit) values(#{fid},#{cid},#{ispass},now(),#{istransit})")
    int salesmanAcceptAdd(SalesmanAccept salesmanAccept);
    //service层根据istransit判断是否验收成功，如果需要中转，就调用该方法，会把货运单状态表里的istransit改为1 、status改为2 代表需要中转
    @Update("update freight_status set status=2,istransit=true where id=#{id}")
    int updateFreightStatus1(Integer id);
    //如果service层判断是否验收成功,不需要需中转，验收成功则还要改变货运状态表里面 istransit=0,不需要中转。status=3 代表收货员已验收成功，需要通知客户取货
    //需要传1个参数 货运单状态表的id
    @Update("update freight_status set istransit=false where id=#{id}")
    int updateFreightStatus2(Integer id);

    //业务员删除验收记录
    @Delete("delete from salesman_accept where id = #{id}")
    int salesmanAcceptDeleteById(Integer id);
    //业务员修改验收记录
    @Update("update salesman_accept set ispass=#{ispass},istransit=#{istransit} where id =#{id}")
    int salesmanAcountUpdate(SalesmanAccept salesmanAccept);
    //业务逻辑层



    @Select("select count(*) from salesman_accept where fid=#{fid}")
    int findSalesmanCountByFid(String fid);
    @Select("select ispass from salesman_accept where fid=#{fid}")
    Boolean findSalesmanIspassByFid(String fid);

    @Select("select count(*) from driver_bill where said=#{id}")
    int findDriverBillById(Integer id);


    //查看所有业务员验收表
    @Select("select * from salesman_accept")
    List<SalesmanAccept> salesmanAcceptFindAll();


    //先根据传的fid判断是否有对应的验收表及验收表是否通过，通过才能填写回执单
    @Select("select count(*) from salesman_accept where fid=#{fid}")
    int selectSalesmanAcceptByFid(String fid);
    @Select("select ispass from salesman_accept where fid=#{fid}")
    Boolean selectSalesmanAcceptIspass(String fid);
    @Select("select count(*) from driver_bill where fid = #{fid}")
    int findDriverBillCcountByFid(String fid);

    //通过验收就填司机回执表

    //增加司机回执表，前端传来一个运输单编号fid,通过它查询货运合同单
    @Select("select * from freight_contract where fid=#{fid}")
    FreightContract freightContractFindByFid(String fid);
    //再通过fid查询运输合同单
    @Select("select * from carriage_contract where fid=#{fid}")
    CarrigeContract carriageContractFindByFid(String fid);
    //通过fid去查业务员验收表
    @Select("select * from salesman_accept where fid=#{fid}")
    SalesmanAccept salesmanAcceptFindByFid(String fid);
    //增加司机回执单，并修改运输合同表里的状态为 回执
    @Insert("insert into driver_bill (cid,diid,fid,said) values (#{cid},#{diid},#{fid},#{said})")
    int insertDriverBill(DriverBill driverBill);
    //修改司机信息表里的status运单状态
    @Update("update driver_information set status = status-1 where id=#{diid}")
    int updateDriverInformation(Integer diid);
    //根据cid 去修改运输单里的状态为4表示为司机回执
    @Update("update freight_status set status=4 where id=#{fsid}")
    int updateFreightStatus3(Integer fsid);

    //删除司机回执表
    @Delete("delete from driver_bill where id=#{id}")
    int deleteDriverBillById(Integer id);


    //修改的内容有 收货人联系电话 和司机是否结算
    //根据传来的收回人电话号码去 fid去查货运合同表,获取receiveid,在去修改收货人电话
    @Update("update customer_in set name=#{name},tel=#{tel} where id=#{id}")
    int updateCustomerIn(CustomerIn customerIn);
    //根据cid去修改司机结算表
    @Update("update driver_settle set isSettle=#{isSettle} where cid = #{cid}")
    int updateDriverSettle(DriverSettle driverSettle);

    //查询所有司机回执表
    //查找司机回执表所有
    @Select("select * from driver_bill")
    List<DriverBill> driverBillFindAll();
    //查询运输合同表通过cid
    @Select("select * from carriage_contract where cid = #{cid}")
    public CarrigeContract carrigeContractFindByCid(String cid);
    //查询司机表通过diid
    @Select("select * from driver_information where id=#{diid} ")
    public DriverInformation driverFindByDiid(Integer diid);
    //查询司机结算表通过cid
    @Select("select isSettle from driver_settle where cid=#{cid}")
    Boolean driver_SettleFindByCid(String cid);
    /*//查询货运单合同表通过fid  上面已经有了
    @Select("select * from freight_contract where fid=#{fid}")
    FreightContract freightContractFindByFid(String fid);*/
    //查询业务员验收表通过said
    @Select("select * from salesman_accept where id=#{said}")
    SalesmanAccept salesmanAcceptFindBySaid(Integer said);
    //查询收获客户信息表通过货运单合同表里的coid
    @Select("select * from customer_in where id =#{coid}")
    CustomerIn customerInFindByCoid(Integer coid);
    //通过fid查客户验收表
    @Select("select count(*) from customer_accept")
    int findCustomerCountByFid(String fid);
    @Select("select ispass from customer_accept where fid=#{fid}")
    Boolean customerAcceptFindByFid(String fid);




}
